function vfile = vmp_SaveAsVMR(hfile, vmrfile, mapno)
% VMP::SaveAsVMR  - saves a VMP as VMR
%
% FORMAT:       [vmr] = vmp.SaveAsVMR(vmrfile [, mapno])
%
% Input fields:
%
%       vmrfile     filename of VMR to write
%       mapno       number of map to write (default: 1)
%
% Output fields:
%
%       vmr         VMR BVQXfile object
%
% Note: currently only up to 256^3 VMR's are supported

% Version:  v0.7b
% Build:    7090215
% Date:     Sep-02 2007, 3:25 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'vmp')
    error( ...
        'BVQXfile:BadArguments', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
if nargin < 2 || ...
   ~ischar(vmrfile) || ...
    isempty(vmrfile)
    vmrfile = '';
else
    vmrfile = vmrfile(:)';
end
if nargin < 3 || ...
   ~isa(mapno, 'double') || ...
    numel(mapno) ~= 1 || ...
    isinf(mapno) || ...
    isnan(mapno) || ...
    mapno < 1 || ...
    mapno > numel(bc.Map) || ...
    mapno ~= fix(mapno)
    mapno = 1;
end

% get position and resolution for data
xpos = bc.XStart + round((256 - bc.VMRDimX) / 2) + 1;
xend = bc.XEnd + round((256 - bc.VMRDimX) / 2) + 1;
ypos = bc.YStart + round((256 - bc.VMRDimY) / 2) + 1;
yend = bc.YEnd + round((256 - bc.VMRDimY) / 2) + 1;
zpos = bc.ZStart + round((256 - bc.VMRDimZ) / 2) + 1;
zend = bc.ZEnd + round((256 - bc.VMRDimZ) / 2) + 1;
pr = bc.Resolution;

% get map range
mapmin = min(bc.Map(mapno).VMPData(:));
mapmax = max(bc.Map(mapno).VMPData(:));

% check range
if isinf(mapmin) || ...
    isnan(mapmin) || ...
    isinf(mapmax) || ...
    isnan(mapmax)
    error( ...
        'BVQXfile:BadFileContent', ...
        'No Inf or NaN in Map supported.' ...
    );
elseif mapmin == mapmax
    mapmax = mapmax + eps;
end
maprng = mapmax - mapmin;

% create new VMR
vfile = BVQXfile('new:vmr');
vc = bvqxfile_getcont(vfile.L);

% fill VMR
try
    map = uint8(25 + round(200 * ...
        (bc.Map(mapno).VMPData - mapmin) / maprng));
    for xc = 1:pr
        for yc = 1:pr
            for zc = 1:pr
                vc.VMRData( ...
                    xpos:pr:xend, ypos:pr:yend, zpos:pr:zend) = map;
            end
        end
    end
    if size(vc.VMRData, 1) > 256
        vc.VMRData(257:end, :, :) = [];
    end
    if size(vc.VMRData, 2) > 256
        vc.VMRData(:, 257:end, :) = [];
    end
    if size(vc.VMRData, 3) > 256
        vc.VMRData(:, :, 257:end) = [];
    end
catch
    bvqxfile_clear(vfile.L);
    error( ...
        'BVQXfile:BadFileContent', ...
        'VMP inner dimensions mismatch.' ...
    );
end

% save VMR?
bvqxfile_setcont(vfile.L, vc);
if ~isempty(vmrfile)
    try
        aft_SaveAs(vfile, vmrfile);
    catch
        warning( ...
            'BVQXfile:ErrorWritingFile', ...
            'Couldn''t write VMR file to disk: ''%s''.', ...
            vmrfile ...
        );
    end
end
